package cn.sunxyz.code.boolean_;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 布尔代数
 * Created by sunxyz on 2017/3/20.
 */
public final class BooleanAlgebra {

    private BooleanAlgebra(){}

    //并联
    public static Boolean or(Boolean b1, Boolean b2) {
        return bool((x, y) -> x || y, b1, b2);
    }

    //串联
    public static Boolean and(Boolean b1, Boolean b2) {
        return bool((x, y) -> x && y, b1, b2);
    }

    //反向器
    public static Boolean non(Boolean b1) {
        return !b1;
    }

    //and(or(x,y),non(and(x,y)))
    public static Boolean xor(Boolean b1, Boolean b2) {
        return and(or(b1,b2),non(and(b1,b2)));//bool((x, y) -> x != y, b1, b2);
    }

    public static Boolean bool(Bool<Boolean> bool, Boolean bol1, Boolean bol2) {
        return bool.test(bol1, bol2);
    }

    @FunctionalInterface
    interface Bool<T> {
        T test(T t1, T t2);
    }
}
